from SOAPpy import *
import SOAPpy
# tmd5.py
import sys
import chilkat
key = 'JSe8gvyh';
encryptpassword = "";
accountauthcate = "";
def testMD5(str):
    import md5  
    import hashlib

    src = str
    m1 = md5.new()   
    m1.update(str+'&'+key)   
    dest1 = m1.hexdigest()
    #m = hashlib.md5()
    #m.update(str);
    #dest1=m.hexdigest();

    #print 'source string: ', src   
    #print 'destination string1: ', dest1
    return dest1

def testMD5Unscard(str):
    import md5  
    import hashlib

    src = str
    m1 = md5.new()   
    m1.update(str)   
    dest1 = m1.hexdigest()
    #m = hashlib.md5()
    #m.update(str);
    #dest1=m.hexdigest();

    #print 'source string: ', src   
    #print 'destination string1: ', dest1
    return dest1

#print testMD5Unscard('13761608169&73E53C799D22618C&JSe8gvyh')

    

def testDES(str,key):

    crypt = chilkat.CkCrypt2()
    success = crypt.UnlockComponent("30-day trial")
    if (success != True):
        print crypt.lastErrorText()
        sys.exit()
        
    crypt.put_CryptAlgorithm("des")
    crypt.put_CipherMode("cbc")
    crypt.put_KeyLength(64)

    #  The padding scheme determines the contents of the bytes
    #  that are added to pad the result to a multiple of the
    #  encryption algorithm's block size.  DES has a block
    #  size of 8 bytes, so encrypted output is always
    #  a multiple of 8.
    crypt.put_PaddingScheme(3)
    

    #  EncodingMode specifies the encoding of the output for
    #  encryption, and the input for decryption.
    #  It may be "hex", "url", "base64", or "quoted-printable".
    crypt.put_EncodingMode("hex")

    #  An initialization vector is required if using CBC or CFB modes.
    #  ECB mode does not use an IV.
    #  The length of the IV is equal to the algorithm's block size.
    #  It is NOT equal to the length of the key.
    ivHex = key;#"JSe8gvyh"
    ivHex='\x00\x00\x00\x00\x00\x00\x00\x00'
    crypt.SetEncodedIV(ivHex,"hex")

    #  The secret key must equal the size of the key.  For
    #  DES, the key must be 8 bytes (i.e. 64-bits).
    keyHex = key;
    crypt.SetEncodedKey(keyHex,"ascii")

    #  Encrypt a string...
    #  The input string is 44 ANSI characters (i.e. 44 bytes), so
    #  the output should be 48 bytes (a multiple of 8).
    #  Because the output is a hex string, it should
    #  be 96 characters long (2 chars per byte).
    encStr = crypt.encryptStringENC(str)
    #print "Encypted string: " +encStr

    #  Now decrypt:
    decStr = crypt.decryptStringENC(encStr)
    print "Decypted string: " +decStr
    return encStr;

def invokeWSDL(str,str_1):
    print 'invoke function'
    

    #just use the path to the wsdl of your choice
    proxy = WSDL.Proxy('http://localhost:8080/unsins/webservice/IUnsinsWebservice?WSDL')
    server = SOAPProxy('http://localhost:8080/unsins/webservice/IunsinsWebService?WSDL');
    
    parameter = "%s&73E53C799D22618C"%(accountauthcate);
    md5string = testMD5(parameter);
    tem =proxy.accountUpdatePassword(parameter+'&'+md5string);
    print "accountUpdatePassword:%s"%(tem)

    parameter = "%s&&"%(accountauthcate);
    md5string = testMD5(parameter);
    tem =proxy.getRecentTransaction(parameter+"&"+md5string);
    print "getRecentTransaction:%s"%(tem)

    parameter = "%s&&"%(accountauthcate);
    md5string = testMD5(parameter);
    tem =proxy.getRecentPayment(parameter+"&"+md5string);
    print "getRecentPayment:%s"%(tem)

    
    return

def testAccountAuthenticate():
    #just use the path to the wsdl of your choice
    proxy = WSDL.Proxy('http://localhost:8080/unsins/webservice/IUnsinsWebservice?WSDL')
    server = SOAPProxy('http://localhost:8080/unsins/webservice/IunsinsWebService?WSDL');
    #server.config.dumpSOAPOut = 0           
    #server.config.dumpSOAPIn = 0
    
    #temp = server.sayHelloRequest("odpsoft");
    #print temp+'==oko'
    
    parameter = "%s"%(accountauthcate);
    md5string = testMD5(parameter);
    
    tem =proxy.accountAuthenticate(parameter+'&'+md5string);
    print "accountAuthenticate:%s"%(tem)
    
def testQueryOrderStateByInsuranceNo():
    proxy = WSDL.Proxy('http://localhost:8080/unsins/webservice/IUnsinsWebservice?WSDL')
    parameter = "2008091100404";
    md5string = testMD5(parameter);
    tem =proxy.queryOrderStateByInsuranceNo(parameter+"&"+md5string);
    print "queryOrderStateByInsuranceNo:%s"%(tem)
    

def testPayment():
    proxy = WSDL.Proxy('http://localhost:8080/unsins/webservice/IUnsinsWebservice?WSDL')
    
    #dev
    #unscardno = '9444446996856589116';
    #unscardpwd='87054500';
    
    #test
    unscardno = '9444448610017260966';
    unscardpwd='000000';
    
    unsins_key = "JSe8gvyh";
    
    
    unscardpwd_des=testDES(unscardpwd,unsins_key);
    parameter = "%s&%s&2008091100399"%(unscardno,unscardpwd_des);
    md5string = testMD5(parameter);
    tem =proxy.checkInOrderByInsuranceNo(parameter+"&"+md5string);
    print "checkInOrderByInsuranceNo:%s"%(tem)


def testChilkatMD5(str):
    import sys
    import chilkat

    crypt = chilkat.CkCrypt2()
    success = crypt.UnlockComponent("30-day trial")
    if (success != True):
        print "Crypt component unlock failed"
        sys.exit()
        
    crypt.put_EncodingMode("Hex")
    crypt.put_Charset("utf8")
    crypt.put_HashAlgorithm("md5")
    hashStr = crypt.hashStringENC(str)

    print hashStr
    
    
def testCardWS():
    unscardno = '9444448610332562542';
    unscardpwd='000000';
    unscard_merchantno='999999999999997';
    unscard_merchant_key='12345678';
    unscard_merchant_terminal='99999996';
    unscard_pwddes= testDES(unscardpwd,unscard_merchant_key);
    
    proxy = WSDL.Proxy('http://192.168.1.7:9080/services/CardWebService?wsdl');
    server = SOAPProxy('http://192.168.1.7:9080/services/CardWebService?wsdl');
    server.config.dumpSOAPOut = 1           
    server.config.dumpSOAPIn = 1
    parameter ="cardNo=%s&merchantNo=%s&cardPWD=%s&amount=12&transId=2009010612350008&transTime=20090106123059"%(unscardno,unscard_merchantno,'A4E6C9864F08115B')
    
    unscardMD5 = testMD5Unscard(parameter)
    temp = proxy.payment(parameter,unscardMD5);
    print parameter;
    
    print temp;
    print unscardMD5
    
    #for method in proxy.methods:
    #    print method
        
    #callinfo = proxy.methods["login"];    
    #print callinfo
    
    #print temp;
    
#testDevCardWS();
#testChilkatMD5('abcdef');
#print testMD5('abcdef');
#test code
unsins_key = "JSe8gvyh";
encryptpassword = testDES('12345600',unsins_key);
#print  encryptpassword
#print 

#test env
#accountauthcate = "13402026316&"+encryptpassword;

#dev env
accountauthcate = "13817405576&"+encryptpassword;
#print accountauthcate

#print encryptpassword;
#B80A17BDEC2DEE79
#invokeWSDL('odpsoft','yhr')
#testAccountAuthenticate();
testQueryOrderStateByInsuranceNo();
#testPayment();

#testUnscardPay();
#6763e0d9524f07d25cfaf8ed7437de0